﻿Public Class SongAlerts

    Private Sub SongAlerts_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Main.tmpStation = Nothing
        Main.tmpArtist = Nothing
        Main.tmpTitle = Nothing
        Me.Dispose()
    End Sub

    Private Sub SongAlerts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''Who cares if there was an error, not me.
        On Error Resume Next
        ''Figure out what form to show the user
        signOnForms()

        ''Try to load STATIONS into the text Autocomplete
        TextBoxAddStation.AutoCompleteSource = AutoCompleteSource.CustomSource
        Dim AutoStation As New AutoCompleteStringCollection()
        Dim Doc As New XmlDocument
        Doc.Load("station_info.xml")
        Dim Nodes As XmlNodeList = Doc.SelectNodes("/lineup/category/genre/channel/*")
        Dim Node As XmlNode
        For Each Node In Nodes
            If Node.Name.Equals("station_name") Then
                AutoStation.Add(Node.InnerText)
            End If
        Next
        TextBoxAddStation.AutoCompleteCustomSource = AutoStation

        ''Try to load ARTISTS into the text autocomplete
        TextBoxAddArtist.AutoCompleteSource = AutoCompleteSource.CustomSource
        Dim AutoArtist As New AutoCompleteStringCollection()
        Dim inHolder As String = Nothing
        Dim songArchive As Array = Split(File.ReadAllText("songinfo.csv"), vbNewLine)
        For Each songLine In songArchive
            Dim songSplit As Array = Split(songLine, ",")
            If songSplit.Length = 4 Then
                AutoArtist.Add(songSplit(2))
                inHolder = inHolder & "-" & songSplit(2)
            End If
        Next
        TextBoxAddArtist.AutoCompleteCustomSource = AutoArtist

        ''Set any info passed in
        If Not Main.tmpStation = Nothing Then
            TextBoxAddStation.Text = Main.tmpStation
        End If
        If Not Main.tmpArtist = Nothing Then
            TextBoxAddArtist.Text = Main.tmpArtist
        End If
        If Not Main.tmpTitle = Nothing Then
            TextBoxAddTitle.Text = Main.tmpTitle
        End If

    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabelStatus.LinkClicked
        Try
            If Main.jConnected Then
                Main.JabberClient1.Close()
                LinkLabelStatus.ForeColor = Color.Blue
                LinkLabelStatus.Text = "Offline"
                Main.jConnected = False
                setVisible()
            Else
                If Not My.Settings.sinetSirenUser.Length > 0 And Not My.Settings.sinetSirenPass.Length > 0 Then
                    MessageBox.Show("You must setup your SiNet Siren account before using Sirens", _
                                    "- SiNet Info Message -", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                Else
                    ''Let's connect
                    Main.JabberClient1.Connect()
                    ''Now, if no autologin then manually set the u/p and call login
                    If Not Main.JabberClient1.AutoLogin Then
                        Main.JabberClient1.User = My.Settings.sinetSirenUser
                        Main.JabberClient1.Password = My.Settings.sinetSirenPass
                        Main.JabberClient1.Login()
                    End If
                End If
            End If
        Catch ex As Exception
            writeLog(4, ex.Message, True)
        End Try
    End Sub
    Public Sub signOnForms()
        If Main.jConnected Then
            LinkLabelStatus.ForeColor = Color.GreenYellow
            LinkLabelStatus.Text = "Online"
            Main.jConnected = True
            setVisible()
            getSirens()
        Else
            LinkLabelStatus.ForeColor = Color.Blue
            LinkLabelStatus.Text = "Offline"
        End If
    End Sub
    Private Sub setVisible()
        GroupBoxExistingSirens.Visible = Main.jConnected
        GroupBoxNewSiren.Visible = Main.jConnected
    End Sub

    Public Sub getSirens()
        Try
            Main.sendMessage("bigboy@" & Main.JabberClient1.Server.ToString, "GET:")
        Catch ex As Exception
            writeLog(4, ex.Message, True)
        End Try
    End Sub

    Public Sub sirenGet()
        Try
            If Main.inMessage.Length > 5 Then
                Main.inMessage = Main.inMessage.Remove(0, 4)
                ListBoxExisting.Items.Clear()
                Dim stationSA As String = Nothing
                Dim artistSA As String = Nothing
                Dim titleSA As String = Nothing
                Main.inMessage = Regex.Replace(Main.inMessage, "(\r(\n)?)|(\n(\r)?)", vbNewLine)
                Dim eachLine As Array = Split(Main.inMessage, vbNewLine)
                For Each sirenLine In eachLine
                    If sirenLine.ToString.Length > 0 Then
                        Dim sirenDetail As Array = Split(sirenLine, "%%")
                        If sirenDetail(0).Contains("NULL88") Then
                            stationSA = "any station"
                        Else
                            stationSA = sirenDetail(0)
                        End If
                        If sirenDetail(1).Contains("NULL88") Then
                            artistSA = "any artist"
                        Else
                            artistSA = sirenDetail(1)
                        End If
                        If sirenDetail(2).Contains("NULL88") Then
                            titleSA = "any title"
                        Else
                            titleSA = sirenDetail(2)
                        End If
                        ListBoxExisting.Items.Add("Station: " & stationSA & " | Artist: " & artistSA & " | Title: " & titleSA)
                    End If
                Next

            Else
                ListBoxExisting.Items.Clear()
            End If
        Catch ex As Exception
            writeLog(4, ex.Message, True)
        End Try
    End Sub

    Private Sub ButtonRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRemove.Click
        Try
            If ListBoxExisting.SelectedIndices.Count > 0 Then
                Dim line As Array = Split(Main.inMessage, vbNewLine)
                For Each value In ListBoxExisting.SelectedIndices
                    Main.sendMessage("bigboy@" & Main.JabberClient1.Server.ToString, "DEL:" & line(value))
                Next
                getSirens()
            Else
                MessageBox.Show("You must select a siren to remove.", "- SiNet Information -", MessageBoxButtons.OK, MessageBoxIcon.Hand)
            End If
        Catch ex As Exception
            writeLog(4, ex.Message, True)
        End Try
    End Sub

    Private Sub ButtonClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClear.Click
        On Error Resume Next
        ListBoxExisting.ClearSelected()
    End Sub

    Private Sub ButtonAddSiren_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAddSiren.Click
        Try
            If TextBoxAddStation.Text = Nothing And TextBoxAddArtist.Text = Nothing And TextBoxAddTitle.Text = Nothing Then
                MessageBox.Show("You must select at least one search criteria.", "- SiNet Info -", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            If TextBoxAddArtist.Text = Nothing And TextBoxAddTitle.Text = Nothing Then
                MessageBox.Show("You must select an artist or a title.", "- SiNet Info -", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            If TextBoxAddStation.Text.Contains("%") Or _
            TextBoxAddArtist.Text.Contains("%") Or _
            TextBoxAddTitle.Text.Contains("%") Then
                MessageBox.Show("You must not use the percent (%) character yet, sorry regex users. Maybe in the next version.", "- SiNet Info -", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            If TextBoxAddStation.Text.ToLower.StartsWith("rx:") Then
                MessageBox.Show("Sorry, you can't Regex a station name.", "- SiNet Info -", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                Exit Sub
            End If
            Dim AddStation As String = Nothing
            Dim AddArtist As String = Nothing
            Dim AddTitle As String = Nothing
            If TextBoxAddStation.Text = Nothing Then
                AddStation = "NULL88"
            Else
                AddStation = TextBoxAddStation.Text
            End If
            If TextBoxAddArtist.Text = Nothing Then
                AddArtist = "NULL88"
            Else
                AddArtist = TextBoxAddArtist.Text
            End If
            If TextBoxAddTitle.Text = Nothing Then
                AddTitle = "NULL88"
            Else
                AddTitle = TextBoxAddTitle.Text
            End If

            Main.sendMessage("bigboy@" & Main.JabberClient1.Server.ToString, "ADD:" & AddStation & "%%" & _
                             AddArtist & "%%" & AddTitle)
        Catch ex As Exception
            writeLog(4, ex.Message, True)
        End Try
    End Sub

End Class